package com.sunday.ble.orlant.utils;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.v7.widget.AppCompatImageView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.sunday.ble.orlant.R;
import com.sunday.common.utils.DeviceUtils;

import java.text.DecimalFormat;

/**
 * Created by littlexs on 2017/6/21.
 */

public class AutoFitImageView extends AppCompatImageView {

    private Context context;
    private int ui_view_h = 100,ui_view_w= 100,ui_root_h = 1334,ui_root_w = 512;//UI设计图view的高度以及UI设计图屏幕高度  px
    private double scaleH,scaleW;// scale = ui_view_h/ui_root_h;
    private int w,h;
    private DecimalFormat df = new DecimalFormat("0.00");

    public AutoFitImageView(Context context) {
        super(context);
        init(context,null);
    }

    public AutoFitImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public AutoFitImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context,AttributeSet attrs){
        this.context = context;
        if (attrs!=null){
            TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AutoFitImageView);
            ui_view_h = a.getInt(R.styleable.AutoFitImageView_ui_view_h, 100);
            ui_view_w = a.getInt(R.styleable.AutoFitImageView_ui_view_w, 100);
            ui_root_h = a.getInt(R.styleable.AutoFitImageView_ui_root_h, 1334);
            ui_root_w = a.getInt(R.styleable.AutoFitImageView_ui_root_h, 1334);
            a.recycle();
        }
        w = DeviceUtils.getDisplay(context).widthPixels;
        h = DeviceUtils.getDisplay(context).heightPixels;
        int navigation = DeviceBytes.getVirtualBarHeigh(context);
        Log.i("---virtualBarHeigh : ",""+navigation);
        Log.i("---AutoFitImageView",ui_view_h+" "+ui_view_w+" "+ui_root_h+" "+ui_root_w);
        if (navigation>0){
            h -= navigation;
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        scaleH = Double.parseDouble(df.format(ui_view_h/ui_root_h));
        scaleW = Double.parseDouble(df.format(ui_view_w/ui_root_w));
        Log.i("---scale : ",scaleH+"  "+scaleW);
        setMeasuredDimension((int)(scaleH*h), (int)(scaleW*w));
    }

    private int getMySize(int defaultSize, int measureSpec) {
        int mySize = defaultSize;

        int mode = MeasureSpec.getMode(measureSpec);
        int size = MeasureSpec.getSize(measureSpec);

        switch (mode) {
            case MeasureSpec.UNSPECIFIED: {//如果没有指定大小，就设置为默认大小
                mySize = defaultSize;
                break;
            }
            case MeasureSpec.AT_MOST: {//如果测量模式是最大取值为size
                //我们将大小取最大值,你也可以取其他值
                mySize = size;
                break;
            }
            case MeasureSpec.EXACTLY: {//如果是固定的大小，那就不要去改变它
                mySize = size;
                break;
            }
        }
        return mySize;
    }
}
